package com.changpay.zywy.ui.activity.machinecontrol

import android.os.Bundle
import android.support.v7.widget.DefaultItemAnimator
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.changpay.zywy.R
import com.changpay.zywy.adapter.ActMachineAdapter
import com.changpay.zywy.adapter.NoActMachineAdapter
import com.changpay.zywy.base.BaseFragment
import com.changpay.zywy.base.IView
import com.changpay.zywy.bean.*
import com.changpay.zywy.ext.sss
import com.changpay.zywy.http.RetrofitHelper

import com.changpay.zywy.wight.SpaceItemDecoration
import kotlinx.android.synthetic.main.fragment_refresh_layout.*


class  MachineFragment : BaseFragment(), IView {
    override fun showLoading() {

    }

    override fun hideLoading() {

    }

    override fun showDefaultMsg(msg: String) {

    }

    override fun showMsg(msg: String) {

    }

    override fun showError(errorMsg: String) {

    }

    private var position: Int = 0  // 当前viewPage的位置
    private lateinit var type: String
    private var page = 1
    private val pagesize = 10

    private var searchKey = ""
    private var goods_id: String? = null
    private var others_member_id: String? = null

    companion object {
        fun getInstance(instanceType: Int,goods_id:String,others_member_id:String?): MachineFragment {
            val bundle = Bundle()
            bundle.putInt("position", instanceType)
            bundle.putString("goods_id", goods_id)
            bundle.putString("others_member_id", others_member_id)
            val fragment = MachineFragment()
            fragment.arguments = bundle
            return fragment
        }
    }


    override fun attachLayoutRes(): Int = R.layout.fragment_refresh_layout

    override fun lazyLoad() {
        loadData()

    }

    override fun initView(view: View) {
        goods_id = arguments?.getString("goods_id")
        others_member_id = arguments?.getString("others_member_id")
        position = arguments?.getInt("position") ?: 0
        type = if (position == 0) "0" else "1"
        swipeRefreshLayout.isEnabled = false
        initRecyclerView()
    }

    /**
     * datas
     */
    private val datas_one = mutableListOf<NoActPartnerMachineBean.ListBean>()

    /**
     * datas
     */
    private val datas_two = mutableListOf<ActPartnerMachineBean.ListBean>()

    /**
     * RecyclerView Divider
     */
    private val recyclerViewItemDecoration by lazy {
        this.let {
            SpaceItemDecoration(context!!)
        }
    }

    /**
     * Adapter
     */
    private val mAdapter_one: NoActMachineAdapter by lazy {
        NoActMachineAdapter(context, datas_one)
    }

    /**
     * Adapter
     */
    private val mAdapter_two: ActMachineAdapter by lazy {
        ActMachineAdapter(context, datas_two)
    }


    /**
     * LinearLayoutManager
     */
    private val linearLayoutManager: LinearLayoutManager by lazy {
        LinearLayoutManager(context)
    }

    /**
     * LoadMoreListener
     */
    private val onRequestLoadMoreListener = BaseQuickAdapter.RequestLoadMoreListener {
        when (position) {
            0 -> {
                page = mAdapter_one.data.size / pagesize + 1
                loadData()
            }
            1 -> {
                page = mAdapter_two.data.size / pagesize + 1
                loadData()
            }
        }

    }

    private fun loadData() {
        if (position == 0) {
            val param = hashMapOf<String, String>()
            param["key"] = token
            param["page"] = page.toString()
            param["pagesize"] = pagesize.toString()
            param["sn_code"] = searchKey
            param["goods_id"] = goods_id.toString()
            if (others_member_id!=null)
            {
                param["others_member_id"] = others_member_id.toString()
            }

            param["machine_type"] = "1"
            RetrofitHelper.service.NoActionMachineToolsList(param).sss(this)
            {
                mAdapter_one.run {
                    addData(it.result.list)
                    val size = it.result.list.size
                    if (size < 10) {
                        loadMoreEnd(true)
                    } else {
                        loadMoreComplete()
                    }
                }
                val act = activity as MachineActivity
                act.changeTabTitle(position, it.result.total_goods_count_num.toString())
            }
        } else {
            val param = hashMapOf<String, String>()
            param["key"] = token
            param["page"] = page.toString()
            param["pagesize"] = pagesize.toString()
            param["sn_code"] = searchKey
            param["goods_id"] = goods_id.toString()
            if (others_member_id!=null)
            {
                param["others_member_id"] = others_member_id.toString()
            }
            param["machine_type"] = "2"
            RetrofitHelper.service.ActionMachineToolsList(param).sss(this)
            {
                mAdapter_two.run {
                    addData(it.result.list)
                    val size = it.result.list.size
                    if (size < 10) {
                        loadMoreEnd(true)
                    } else {
                        loadMoreComplete()
                    }
                }
                val act = activity as MachineActivity
                act.changeTabTitle(position, it.result.total_goods_count_num.toString())
            }
        }
    }


    /**
     * ItemClickListener
     */
    private val onItemClickListener = BaseQuickAdapter.OnItemClickListener { _, _, position ->
        when (position) {
            0 -> if (datas_one.size != 0) {
                val data = datas_one[position]
//            Intent(activity, ContentActivity::class.java).run {
//                putExtra(Constant.CONTENT_URL_KEY, data.link)
//                putExtra(Constant.CONTENT_TITLE_KEY, data.title)
//                putExtra(Constant.CONTENT_ID_KEY, data.id)
//                startActivity(this)
//            }
            }
            1 -> if (datas_two.size != 0) {
                val data = datas_two[position]
//            Intent(activity, ContentActivity::class.java).run {
//                putExtra(Constant.CONTENT_URL_KEY, data.link)
//                putExtra(Constant.CONTENT_TITLE_KEY, data.title)
//                putExtra(Constant.CONTENT_ID_KEY, data.id)
//                startActivity(this)
//            }
            }
        }


    }

    /**
     * ItemChildClickListener
     */
    private val onItemChildClickListener = BaseQuickAdapter.OnItemChildClickListener { _, view, position ->

        when (position) {
            0 -> if (datas_one.size != 0) {
                val data = datas_one[position]
                when (view.id) {
//                    R.id.iv_like -> {
//
//                    }
                }
            }
            1 -> if (datas_two.size != 0) {
                val data = datas_two[position]
                when (view.id) {
//                    R.id.iv_like -> {
//
//                    }
                }
            }
        }
    }


    private fun initRecyclerView() {
        recyclerView.run {
            layoutManager = linearLayoutManager
            when (position) {
                0 -> adapter = mAdapter_one
                1 -> adapter = mAdapter_two
            }
            itemAnimator = DefaultItemAnimator()
//            recyclerViewItemDecoration.let { addItemDecoration(it) }
        }
        val adapter = when (position) {
            0 -> mAdapter_one
            1 -> mAdapter_two
            else -> mAdapter_one
        }
        adapter.run {
            bindToRecyclerView(recyclerView)
//            setOnLoadMoreListener(onRequestLoadMoreListener, recyclerView)
            onItemClickListener = this.onItemClickListener
            onItemChildClickListener = this.onItemChildClickListener
            setEmptyView(R.layout.fragment_empty_layout)
        }
    }
}






